<?php
namespace Admin\Controller;

class UserController extends CommonController{
    
    public function index(){
        $User = D($this->controller);
        $Hospital = D('Hospital');
        $Weight = M('Weight');
        //城市数组
        $this->assign('cityRow',D('HptCity')->getCityRow());
        
        $where = array();
        $where['type'] = I('get.type');
        $where['name'] = array('neq','');
        if(I('get.keywords')){
            $where['name'] = array('like','%'.I('get.keywords').'%');
        }
        $dataPage = $User->getPager($where);
        $list = $dataPage['data'];
        
        //数据修整
        foreach($list as $k=>$v){
            if(I('get.type') != '3'){
                //查出医院名
                $hptRow = $Hospital->find(intval($v['hpt_id']));
                $list[$k]['hpt_name'] = $hptRow['name'];
            }
            if(I('get.type') == '1'){//医生列表显示患者数/达标数
                $sefeWeightNum = 0;//超重人数
                $patientList = $User->where(array('type'=>'2','uid'=>$v['id'],'name'=>array('neq','')))->select();
                foreach($patientList as $pv){
                    $weightRow = $Weight->where(array('uid'=>$pv['id']))->order('id desc')->find();
                    if($pv['weight_safe'] > $weightRow['vol']) $sefeWeightNum++;
                }
                $list[$k]['patientNum'] = count($patientList);
                $list[$k]['sefeWeightNum'] = $sefeWeightNum;
            }
        }
        
        $this->assign('list',$list);
        $this->assign('page',$dataPage['page']);
        if(I('get.type') == '2'){
            $this->display('index_patient');
        }else{
            $this->display();
        }
    }
    
    public function edit(){
        if(IS_POST){
            $data = I('post.');
            //添加时账号不重复
            if(empty($data['info']['id'])){
                if($row = D($this->controller)->where(array('phone'=>$data['info']['phone']))->find()) die('cf');
            }
            
            if($res = D($this->controller)->saveData($data)){
                die('suc');
            }else{
                die('err');
            }
        }else{
            $this->assign('cityRow',D('HptCity')->getCityRow());//城市
            
            $id=I('get.id');
            if($id){
                $this->assign('vo',D($this->controller)->find($id));
            }
            $this->assign('upPageUrl',$_SERVER['HTTP_REFERER']);
            $this->display();
        }
    }
    
    //患者详细信息
    public function info_patient(){
        $this->assign('cityRow',D('HptCity')->getCityRow());//城市
        $id=I('get.id');
        $this->assign('vo',D($this->controller)->find($id));
        $this->display();
    }
    
    //名下患者
    public function patient_list(){
        $field = I('get.type')=='1' ? 'uid' : 'nid';
        $this->assign('list',D($this->controller)->where(array($field=>I('get.id'),'name'=>array('neq','')))->select());
        $this->display();
    }
    
    //医生或营养师与某个患者的聊天记录
    public function chat(){
        $User = D($this->controller);
        $docPhone = $User->getPhoneById(I('get.did'));
        $patPhone = $User->getPhoneById(I('get.pid'));
        
        $where = "(sender = '{$docPhone}' and receiver = '{$patPhone}') or (receiver = '{$docPhone}' and sender = '{$patPhone}')";
        $dataPage = D('Chat')->getPager($where);
        foreach($dataPage['data'] as $k=>$v){
            $dataPage['data'][$k]['name'] = ($docPhone == $v['sender']) ? '医生' : '患者';
        }
        $this->assign('list',$dataPage['data']);
        $this->assign('page',$dataPage['page']);
        $this->display();
    }
    
    //增加积分
    public function addScore(){
        D($this->controller)->where(array('id'=>I('post.id')))->setInc('score',intval(I('post.score')));
        M('Score')->add(array(
            'uid'=>I('post.id'),'type'=>'no',
            'score'=>intval(I('post.score')),
            'reason'=>I('post.reason'),'create_date'=>date('Y-m-d')
        ));
    }
    
}